// pages/Details/Details.js
const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      subject:{},
      checkpoint:0,
      pic:"",
      word1:{
        id:"",
        word:"",
        windex:""
      },
      word2:{
        id:"",
        word:"",
        windex:""
      },
      word3:{
        id:"",
        word:"",
        windex:""
      },
      word4:{
        id:"",
        word:"",
        windex:""
      },
      answer:""
  },
    //广告跳转
    avdtap:function (e) {
        wx.navigateToMiniProgram({
            appId: e.currentTarget.dataset.appid,
            path: 'pages/index/index',
            success:function(res) {
                console.log(res)
            }
        })
    },
    //回答问题
  optionBindtap:function (e) {
      var that = this
      var opt = e.currentTarget.dataset
      var index = opt.index
      var data = that.data.option
      if(that.data.word1.id===""||that.data.word2.id===""||that.data.word3.id===""||that.data.word4.id===""){
      data[index].word = ''
      data[index].id = ''
      that.setData({
          option: data,
      })
        if(that.data.word1.id===""){
            that.setData({
                word1:{
                    id:e.currentTarget.dataset.id,
                    word:e.currentTarget.dataset.word,
                    windex:e.currentTarget.dataset.index
                }
            })
        }else if(that.data.word2.id===""){
            that.setData({
                word2:{
                    id:e.currentTarget.dataset.id,
                    word:e.currentTarget.dataset.word,
                    windex:e.currentTarget.dataset.index
                }
            })

        }else if(that.data.word3.id===""){
            that.setData({
                word3:{
                    id:e.currentTarget.dataset.id,
                    word:e.currentTarget.dataset.word,
                    windex:e.currentTarget.dataset.index
                }
            })
        }else{
            that.setData({
                word4:{
                    id:e.currentTarget.dataset.id,
                    word:e.currentTarget.dataset.word,
                    windex:e.currentTarget.dataset.index
                }
            })
           var word = that.data.word1.word+that.data.word2.word+that.data.word3.word+e.currentTarget.dataset.word
            console.log(word)
            if(that.data.answer==word){
                app.globalData.userInfo.checkpoint = that.data.checkpoint
                that.update()
                wx.showModal({
                    title: "回答正确",
                    content: "正确答案:"+word,
                    showCancel: false,
                    confirmText: "确定",
                    success: function(res) {
                        if (res.confirm) {
                            //console.log('用户点击确定')
                            that.onLoadWord(that.data.data)
                        } else if (res.cancel) {
                            //console.log('用户点击取消')
                        }
                    }
                })
            }else{
                that.Refresh()
                wx.showToast({
                    title: '回答错误!',
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
        }
    }
  },
  //修改答案
  answerWord:function (e) {
      //console.log(e.currentTarget,e.currentTarget.dataset)
     var that = this
      var opt = e.currentTarget.dataset
      var index = opt.windex
      var data = that.data.option
      data[index].word = e.currentTarget.dataset.word
      data[index].id = e.currentTarget.dataset.id
      that.setData({
          option: data
      })
      if(e.currentTarget.dataset.type == 1){
          that.setData({
              word1:{
                  id:"",
                  word:""
              }
          })
      }else if(e.currentTarget.dataset.type == 2){
          that.setData({
              word2:{
                  id:"",
                  word:""
              }
          })
      }else if(e.currentTarget.dataset.type ==3){
          that.setData({
              word3:{
                  id:"",
                  word:""
              }
          })
      }else{


      }

  },
    update:function () {
        var that = this
        var NewTime = new Date().getTime().toString();
        var key = util.sha1(app.globalData.userInfo.id + NewTime + app.globalData.userInfo.checkpoint + app.globalData.userInfo.id);
        key = key.toUpperCase()
        var data = {}
        console.log('update群场景shareTicket:',app.globalData)
        var update = function (data) {
            wx.request({
                url: 'https://caige1.sanliwenhua.com/idiom/update_checkpoint',
                method: "post",
                data: data,
                success: function (res) {
                    console.log(res)
                    app.globalData.group_id = res.data.group_id;
                },
                fail: function (res) {
                    console.log(res)
                    wx.showToast({
                        title: res.data.message,
                        icon: 'success',
                        duration: 2000
                    })
                }
            })
        }
        if(app.globalData.shareTicket){
            wx.getShareInfo({
                shareTicket:app.globalData.shareTicket,
                success:function (res) {
                    console.log(res)
                    app.globalData.Sharing= {}
                    app.globalData.Sharing.errMsg = res.errMsg
                    app.globalData.Sharing.encryptedData = res.encryptedData
                    app.globalData.Sharing.iv = res.iv
                    console.log(app.globalData.Sharing)
                    data = {
                        key: key,
                        time: NewTime,
                        checkpoint: app.globalData.userInfo.checkpoint,
                        x_token: app.globalData.userInfo.token,
                        errMsg: res.errMsg,
                        encryptedData: res.encryptedData,
                        iv: res.iv,
                        session_key: app.globalData.userInfo.session_key
                    }
                    update(data);
                },
                fail:function (res) {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'success',
                        duration: 2000
                    })
                }
            })
        }else{
            data = {
                key: key,
                time: NewTime,
                checkpoint: app.globalData.userInfo.checkpoint,
                x_token: app.globalData.userInfo.token
            }
            update(data);
        }
    },
    //重置答案
   Refresh:function(){
       this.setData({
           word1:{
               id:"",
               word:""
           },
           word2:{
               id:"",
               word:""
           },
           word3:{
               id:"",
               word:""
           },
           word4:{
               id:"",
               word:""
           },
           option:this.data.option2
       })
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this
      wx.showLoading({
          title: '加载中...',
      })
      wx.request({
          url: 'https://res7.sanliwenhua.com/picture.json',
          header: {
              'content-type': 'application/json' // 默认值
          },
          success: function (res) {
              console.log(res.data)
              var data = res.data
              wx.hideLoading()
              that.setData({
                  data:data
              })
              that.onLoadWord(data)
          }
      })
      this.adver()
  },
    adver:function () {
        var that = this
        wx.request({
            url: 'https://caige1.sanliwenhua.com/all_advs',
            method: "get",
            success: function (res) {
                //console.log(res, "广告")
                if (res.data.code = 200) {
                    var AdvData = res.data.data;
                    that.setData({
                        avdUrl: AdvData//[0].pic,
                    })
                }
            }
        })
        wx.request({
            url: 'https://caige1.sanliwenhua.com/switch',
            method: "get",
            success: function (res) {
                //console.log(res, "开关")
                if (res.data.code = 200) {
                    var isADV = res.data.data.isADV;
                    that.setData({
                        isADV: isADV //广告开关写死
                    })
                } else {

                }
            }
        })

    },
    onLoadWord:function (e) {
        var that = this
        that.setData({
            word1:{
                id:"",
                word:""
            },
            word2:{
                id:"",
                word:""
            },
            word3:{
                id:"",
                word:""
            },
            word4:{
                id:"",
                word:""
            },
            checkpoint: app.globalData.userInfo.checkpoint+1
        })
        var data = e
        var a = that.data.checkpoint
        function randomSort(a, b) {
            return Math.random() > 0.5 ? -1 : 1;
        }
        var op = []
        var option =[]
        console.log(data[a])
        op = data[a].word.split("");
        for(var i=0;i<op.length;i++){
            var h = {id:i,word:op[i]}
            option.push(h)
        }
        option = option.sort(randomSort);
        var subject = data[a]
        that.setData({
            option2:option,
            option:option,
            subject:data[a],
            pic:data[a].pic,
            answer:data[a].answer
        })
    },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      wx.showShareMenu({
          withShareTicket: true
      })
  },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        var that = this;
        var nickeName = app.globalData.userInfo.nickName;
        if (res.from === 'button') {
            return {
                title: '['+nickeName+'@我],那些年学过的成语，还记得吗？快来挑战吧！',
                path: 'pages/index/index',
                //imageUrl: 'http://paz0eiqln.bkt.clouddn.com/image/fenxing.jpg',
                success: function (res) {
                    console.log('分享到群的标记', res)
                    if(res.shareTickets){
                        wx.showModal({
                            title: "求助成功!",
                            content: "正确答案:"+that.data.answer,
                            showCancel: false,
                            confirmText: "确定"
                        })
                    }else{
                        wx.showModal({
                            title: "提示",
                            content: "分享到微信群才可以哦！",
                            showCancel: false,
                            confirmText: "确定"
                        })
                    }
                },
                fail: function (res) {
                    wx.showToast({
                        title: '分享失败！',
                        image: '/image/errIcon.png',
                        duration: 2000
                    })
                }
            }

        }else{
            return {
                title: '['+nickeName+'@我],那些年学过的成语，还记得吗？快来挑战吧！',
                path: 'pages/index/index',
                imageUrl: 'http://paz0eiqln.bkt.clouddn.com/image/fenxing.jpg',
                success: function (res) {
                    wx.showToast({
                        title: '分享成功!',
                        icon: 'success',
                        duration: 2000
                    })
                },
                fail: function (res) {
                    wx.showToast({
                        title: '分享失败！',
                        image: '/image/errIcon.png',
                        duration: 2000
                    })
                }
            }

        }


    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },


})